package com.liyuan.yiqixing.service.impl;

import com.liyuan.yiqixing.Utils;
import com.liyuan.yiqixing.dao.AuthenticationDao;
import com.liyuan.yiqixing.dao.CommentDao;
import com.liyuan.yiqixing.dao.DiscussionDao;
import com.liyuan.yiqixing.dao.ReplyDao;
import com.liyuan.yiqixing.model.Comment;
import com.liyuan.yiqixing.model.User;
import com.liyuan.yiqixing.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    SimpleDateFormat simpleDateFormat;
    @Autowired
    CommentDao commentDao;
    @Autowired
    ReplyDao replyDao;
    @Autowired
    AuthenticationDao authenticationDao;

    @Autowired
    DiscussionDao discussionDao;

    @Override
    public void insertComment(Comment comment) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal.equals("anonymousUser")) {
            System.out.println(1);
        } else {
//            comment.setUserCommented(1);
            comment.setUserComment(((User) principal).getId());
            comment.setModifiedAt(simpleDateFormat.format(new Date()));
            commentDao.insertComment(comment);
        }
    }

    @Override
    public Comment[] retrieveComments(Integer type, Integer targetId) {
        Comment[] comments = commentDao.selectCommentsByTypeAndTargetId(type, targetId);
        for (Comment comment : comments) {
            completeUserInfo(comment);
        }
        return comments;
    }

    private void completeUserInfo(Comment comment) {
        User userComment = authenticationDao.retrieveUserById(comment.getUserComment().getId());
        comment.getUserComment().setAvatarUrl(userComment.getAvatarUrl());
        comment.getUserComment().setRole(userComment.getRole().getId());
        comment.getUserComment().setLevel(userComment.getLevel());
        comment.getUserComment().setDescription(userComment.getDescription());
        comment.getUserComment().setNickname(userComment.getNickname());

        User userCommented = authenticationDao.retrieveUserById(comment.getUserCommented().getId());
        comment.getUserCommented().setAvatarUrl(userCommented.getAvatarUrl());
        comment.getUserCommented().setRole(userCommented.getRole().getId());
        comment.getUserCommented().setLevel(userCommented.getLevel());
        comment.getUserCommented().setDescription(userCommented.getDescription());
        comment.getUserCommented().setNickname(userCommented.getNickname());
    }

    /**
     * 获取用户的消息，即谁回复了当前用户
     * @param userCommented
     * @return
     */
    @Override
    public Comment[] retrieveCommentsByUserCommentedId(Integer userCommented) {
        Comment[] comments = commentDao.selectCommentsByUserCommentedId(userCommented);
        for(Comment comment:comments){
            completeUserInfo(comment);
        }
        return comments;
    }

    @Override
    public String retrieveCommentContentByTargetIdAndType(Integer targetId, Integer type) {
        String ans=null;
        if(type.equals(Utils.EnumUtils.REPLY_TO_DISCUSSION.getType())){
            ans = discussionDao.selectContent(targetId);//选取discussion的标题
        }else if(type.equals(Utils.EnumUtils.REPLY_TO_COMMENT.getType())){
            ans = commentDao.selectContent(targetId);
        }
        return ans;
    }

    @Override
    public Integer retrieveCommentTargetIdById(Integer targetId) {
        return commentDao.selectCommentTargetIdById(targetId);
    }
}
